//https://leetcode.cn/problems/subarray-sums-divisible-by-k/

class Solution {
public:
    int subarraysDivByK(vector<int>& nums, int k) {
        int n = nums.size();

        unordered_map<int, int> hash;
        int ret = 0, sum = 0;
        hash[0 % k] = 1;
        for (auto x : nums)
        {
            sum += x;
            if (hash.count((sum % k + k) % k))
                ret += hash[(sum % k + k) % k];
            hash[(sum % k + k) % k]++;
        }

        return ret;

    }
};